package com.daichi.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSON;
import com.daichi.yygh.cmn.client.DictFeignClient;
import com.daichi.yygh.hosp.repository.HospitalRepository;
import com.daichi.yygh.hosp.service.HospitalService;
import com.daichi.yygh.model.hosp.Hospital;
import com.daichi.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    private final HospitalRepository hospitalRepository;

    private final DictFeignClient dictFeignClient;

    @Autowired
    public HospitalServiceImpl(HospitalRepository hospitalRepository, DictFeignClient dictFeignClient) {
        this.hospitalRepository = hospitalRepository;
        this.dictFeignClient = dictFeignClient;
    }


    @Override
    public void save(Map<String, Object> paramMap) {
        Hospital hospital = JSON.parseObject(JSON.toJSONString(paramMap), Hospital.class);
        // 判断是否存在
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());

        if (hospitalExist != null) {
            hospitalExist.setUpdateTime(new Date());
            hospitalExist.setIsDeleted(0);
            hospitalRepository.save(hospitalExist);
        } else {
            hospital.setStatus(0);
            hospital.setIsDeleted(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hoscode) {

        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;

    }

    @Override
    public Page<Hospital> selectHospitalPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        // 创建pageable对象
        Pageable pageable = PageRequest.of(page - 1, limit);

        // 创建matcher
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);

        // 创建example
        Example<Hospital> example = Example.of(hospital, matcher);

        Page<Hospital> hospitalPage = hospitalRepository.findAll(example, pageable);

        // 查询结果添加数据字典名称
        hospitalPage.getContent().forEach(this::setHospitalType);

        return hospitalPage;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> getHospById(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        this.setHospitalType(hospital);

        HashMap<String, Object> map = new HashMap<>();
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);

        return map;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        List<Hospital> list = hospitalRepository.getHospitalByHosnameLike(hosname);
        return list;
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        setHospitalType(hospital);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        resultMap.put("hospital",hospital);

        return resultMap;
    }

    private void setHospitalType(Hospital hospital) {

        // 查询医院等级名称
        String hostype = dictFeignClient.getName("Hostype", hospital.getHostype());
        hospital.getParam().put("hostypeString", hostype);

        // 查询 省 市 区 信息
        String province = dictFeignClient.getName(hospital.getProvinceCode());
        String city = dictFeignClient.getName(hospital.getCityCode());
        String district = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("fullAddress", province + city + district);

    }
}
